<?php
/**
 * 线下报告
 */
namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\wx\ReportModel;
use bc\util\backend\Page;
use bc\model\wx\report\ResultModel;
use bc\util\Uploader;
use bc\util\Sms;
use yii;

class OfflineController extends BackendController
{
	
	/**
	 * 列表页
	 */
	public function actionIndex()
	{
		
		$name = $this->get('name', '');
		$mobile = $this->get('mobile', '');
		
		$model = new ReportModel();
		$params = 'openid = ""';
		
		if($name)
		{
			$params .= ' AND `name` = "' . $name . '"';
		}
		
		if($mobile)
		{
			$params .= ' AND `mobile` = "' . $mobile . '"';
		}
		
		$page = $this->get('page', 1);
		$model->sort = 'report_id DESC';
		$list = $model->getList($params, $page, self::PAGE_SIZE);
		
		$total = $model->getTotal($params);
		
		$pagination = new Page($total, self::PAGE_SIZE);
		
		$view['list'] = $list;
		$view['pagination'] = $pagination;
		$view['name'] = $name;
		$view['mobile'] = $mobile;
		return $this->render('index', $view);
		
	}
	
	
	/**
	 * 新增
	 */
	public function actionAdd()
	{
		$reslutModel = new ResultModel();
		$reslutModel->sort = 'report_result_id ASC';
		$reslutList = $reslutModel->getList(['status' => 1]);
		
		$view['result_list'] = $reslutList;
		
		return $this->render('add', $view);
	}
	
	/**
	 * 编辑
	 */
	public function actionEdit()
	{
		$id = $this->get('id');
		
		if(!$id)
			$this->redirect('/');
		
		$model = new ReportModel();
		$row = $model->getRow(['report_id' => $id]);
		
		$reslutModel = new ResultModel();
		$reslutModel->sort = 'report_result_id ASC';
		$reslutList = $reslutModel->getList(['status' => 1]);
		
		$view['row'] = $row;
		$view['result_list'] = $reslutList;
		
		return $this->render('edit', $view);
	}
	
	
	/**
	 * 数据保存
	 */
	public function actionSave()
	{
		$model = new ReportModel();
		if($this->getRequest()->isPost == true)
		{
		
			$id = $this->post('id');
			//附件上传
			if($_FILES['file'])
			{
				$up = new Uploader( 'file' , $this->getUploadConfig() );
				$info = $up->getFileInfo();
				if($info['state'] === 'SUCCESS')
				{
					$data['file'] = str_replace(UPLOAD_PATH, '', $info['url']);
				}
				
				if(!$id)
				{
					//新建报告的时候才发短信
					$text = '亲爱的准妈妈'. $this->post('name')  .'，您可以马上打开微信“芝士妈妈在线”，在菜单“服务台->查询报告”查看您的无创产前检测报告。祝您阖家幸福！';
					$sms = new Sms();
					$sms->setMobile($this->post('mobile'));
					$sms->setText('欢迎使用芝士妈妈无创检测查询！');
					$result = $sms->send();
				}
				
			}
			
			$data['name'] = $this->post('name');
			$data['mobile'] = $this->post('mobile');
			$data['report_result_id'] = $this->post('report_result_id');
			$data['content'] = $this->post('content');
			
			if($id)
			{
				$model->modify($data, $id);
			}
			else
			{
				$data['create_time'] = time();
				$model->add($data);
			}
			$output['msg'] = '操作成功!';
		}
		else
		{
			$output['msg'] = '非法请求!';
		}
		
		$output['url'] = '/offline';
		$this->forward($output);
		
	}
	
	
	/**
	 * @return array
	 */
	private function getUploadConfig()
	{
		//上传文件目录
		$path = UPLOAD_PATH . 'upload/report/';
		//上传配置
		$config = array(
				"savePath" => $path ,             //存储文件夹
				"maxSize" => 100000000 ,                   //允许的文件最大尺寸，单位KB
				"allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp", ".pdf" )  //允许的文件格式
		);
			
		//背景保存在临时目录中
		$config[ "savePath" ] = $path;
		return $config;
	}
	
}

